package com.example;

import com.wholefarm.model.PublicReleaseConfiguration;
import org.apache.ibatis.session.SqlSession;
import public_util.model.ReturnData;
import public_util.pack_4_stable.pay.weichat.WeiXinTool;
import system_api_4_trade.SpecialApplicationBefore;
import system_api_4_trade.util.BeforeTool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * 新系统将API扩展成Before和After方法，这里是Before方法完成相关的操作
 */
public class SpecialApplicationBefore4Example extends SpecialApplicationBefore {



    @Override
    public ReturnData public_page_4m_shop_goods_list_2_reg(HttpServletRequest request, SqlSession session, HashMap<String, Object> param2db, ReturnData resultData) {
        ReturnData checkReturn = BeforeTool.common_verify_invoke_method(request, param2db);
        if (checkReturn != null && checkReturn.getStatus() != 0)
        {
            return checkReturn;
        }
        tutorial.lite_mod.tag_4_goods.TemplateUtilApi.pre_cmd_para_show_tag_goods(request, session, param2db, resultData);
        return null;
    }


    @Override
    public ReturnData public_page_4m_load_agent_list(HttpServletRequest request, ReturnData last_return, SqlSession session, HashMap<String, Object> param2db) {
        ReturnData checkReturn = BeforeTool.common_verify_invoke_method(request, param2db);
        if (checkReturn != null && checkReturn.getStatus() != 0)
        {
            return checkReturn;
        }
        tutorial.lite_mod.human_res.TemplateUtilApi.pre_cmd_para_filter_4_mid_table_remark_025(request, session, param2db);
        return null;
    }

    public ReturnData public_page_4m_load_user_list(HttpServletRequest request, SqlSession session, HashMap<String, Object> param2db, ReturnData resultData) {
        ReturnData checkReturn = BeforeTool.common_verify_invoke_method(request, param2db);
        if (checkReturn != null && checkReturn.getStatus() != 0)
        {
            return checkReturn;
        }
        tutorial.lite_mod.human_res.TemplateUtilApi.pre_cmd_para_filter_4_mid_table_remark_002(request, session, param2db);
        return null;
    }


    @Override
    public ReturnData public_page_4m_load_goods_list(HttpServletRequest request, ReturnData returnData, SqlSession sqlSession, HashMap<String, Object> param2db) {
        ReturnData checkReturn = BeforeTool.common_verify_invoke_method(request, param2db);
        if (checkReturn != null && checkReturn.getStatus() != 0)
        {
            return checkReturn;
        }
        tutorial.lite_mod.tag_4_goods.TemplateUtilApi.pre_cmd_para_show_tag_goods(request, sqlSession, param2db, returnData);
        return null;
    }


    /**
     * /duzikai_api_weichat_menu_opt_wholefarm?action=1&id_public_data_admin_user=-1
     * @param request
     * @param response
     * @param releaseConf
     * https://note.youdao.com/s/AdmM2S4
     * @throws IOException
     */
    @Override
    public void createMenu(HttpServletRequest request, HttpServletResponse response, PublicReleaseConfiguration releaseConf) throws IOException {
        String token = WeiXinTool.getAccess_token("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential", releaseConf.getWC_APPID(), releaseConf.getWC_SECRET());
        response.getWriter().println("---获得Token结果:---" + token);
        //https://jinglintu.downline.cn/004_mobile_web_site_sample_vue02/dist/front_page_index.html#/middle?jump_type=701&place_code=wxfx_0001&id_public_agent_info=99101&id_public_data_admin_user=800&new_page=front_page_index.html%23%2Ffront_page_index&new_page_para=redirect-_-my
        String main = releaseConf.getMAIN_URL();
        String code_4_enter = URLEncoder.encode("front_page_index.html#/front_page_index", "UTF-8");//android必须要编码, 后面参考这个来
        String code_4_user_center = URLEncoder.encode("front_page_index.html#/front_page_index", "UTF-8");//android必须要编码, 后面参考这个来
        String url_4_main_app = main + "004_mobile_web_site_sample_vue02/dist/front_page_index.html#/middle?jump_type=701&id_public_data_admin_user=800&id_public_agent_info=99101&new_page_para=redirect-_-home&new_page=" + code_4_enter;
        String url_4_user_center = main + "004_mobile_web_site_sample_vue02/dist/front_page_index.html#/middle?jump_type=701&id_public_data_admin_user=800&id_public_agent_info=99101&new_page_para=redirect-_-my&new_page=" + code_4_user_center;
        String STR_001 = "{\"name\":\"案例测试\",\"type\":\"view\", \"url\":\"" + url_4_main_app + "\"}";
        String STR_002 = "{\"name\":\"个人中心\",\"type\":\"view\", \"url\":\"" + url_4_user_center + "\"}";
        String STR_003 = "{\"name\":\"样板小程\",\"url\":\"" + releaseConf.getMAIN_URL() + "\",\"type\":\"miniprogram\",\"appid\":\"" + releaseConf.getLITE_PROGRAM_NAKE_APP_ID() + "\", \"pagepath\":\"pages/index/index\"}";
        String user_define_menu = "{\"button\":[ " + STR_001 + "," + STR_002 + "," + STR_003 + "]}";
        logger.info("user_define_menu:" + user_define_menu);
        String res = WeiXinTool.createMenu(token, new String(user_define_menu.getBytes("utf-8")));
        response.getWriter().println("---创建菜单结果:---" + res);
    }
}
